package com.example.zhangjy.collweather.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.zhangjy.collweather.model.City;
import com.example.zhangjy.collweather.model.County;
import com.example.zhangjy.collweather.model.Province;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dell on 2015/8/27.
 */
public class CollWeatherDB {

    /**
     * 数据库名
     */
    public static final String DB_NAME = "coll_weather";

    /**
     * 数据库版本
     */
    public static final int VERSION = 1;

    private static CollWeatherDB collWeatherDB;

    private SQLiteDatabase db;

    private CollWeatherDB(Context context) {

        CollWeatherOpenHelper dbHelper = new CollWeatherOpenHelper(context,DB_NAME,null,VERSION);

        db = dbHelper.getWritableDatabase();
    }


    /**
     * 获取CollWeatherDB单例
     * @param context
     * @return
     */
    public synchronized static CollWeatherDB getInstance(Context context) {
        if(collWeatherDB == null)
            collWeatherDB = new CollWeatherDB(context);

        return collWeatherDB;
    }


    /**
     * 将Province实例存储到数据库中
     * @param province
     */
    public void saveProvince(Province province) {

        if(province != null) {

            ContentValues contentValues = new ContentValues();
            contentValues.put("province_name",province.getProvinceName());
            contentValues.put("province_code",province.getProvinceCode());

            db.insert("province",null,contentValues);
        }

    }

    /**
     * 从数据库读取全国所有的省份信息
     * @return
     */
    public List<Province> loadProvinces() {

        List<Province> provinces = new ArrayList<Province>();
        Cursor cursor = db.query("province",null,null,null,null,null,null);

        if(cursor.moveToFirst()) {
            do {
                Province province = new Province();
                province.setId(cursor.getInt(cursor.getColumnIndex("id")));
                province.setProvinceName(cursor.getString(cursor.getColumnIndex("province_name")));
                province.setProvinceCode(cursor.getString(cursor.getColumnIndex("province_code")));

                provinces.add(province);
            } while (cursor.moveToNext());
        }

        return provinces;
    }

    /**
     *  将City实例存储到数据库中
     */
    public void saveCity(City city){

        if(city != null) {
            ContentValues contentValues = new ContentValues();
            contentValues.put("city_name",city.getCityName());
            contentValues.put("city_code",city.getCityCode());
            contentValues.put("province_id",city.getProvinceId());

            db.insert("city",null,contentValues);
        }
    }

    /**
     * 从数据库中加载city数据
     * @param provinceId 省份ID
     * @return
     */
    public List<City> loadCitys(int provinceId) {

        List<City> citys = new ArrayList<City>();

        Cursor cursor = db.query("city",null,"province_id = ?",
                new String[]{String.valueOf(provinceId)},null,null,null);

        if(cursor.moveToFirst()) {
            do {
                City city = new City();
                city.setId(cursor.getInt(cursor.getColumnIndex("id")));
                city.setCityCode(cursor.getString(cursor.getColumnIndex("city_code")));
                city.setCityName(cursor.getString(cursor.getColumnIndex("city_name")));
                city.setProvinceId(provinceId);

                citys.add(city);
            }while (cursor.moveToNext());
        }
        return citys;
    }

    /**
     * 保存County到数据库中
     * @param county
     */
    public void saveCounty(County county) {
        if (county != null) {
            ContentValues contentValues = new ContentValues();
            contentValues.put("county_name",county.getCountyName());
            contentValues.put("county_code",county.getCountyCode());
            contentValues.put("city_id",county.getCityId());

            db.insert("county",null,contentValues);
        }
    }

    /**
     * 加载County数据
     * @param cityId 市的id
     * @return
     */
    public List<County> loadCounty(int cityId) {
        List<County> counties = new ArrayList<County>();
        Cursor cursor = db.query("county",null,"city_id = ?",
                new String[]{String.valueOf(cityId)},null,null,null);
        if(cursor.moveToFirst()) {
            do {
                County county = new County();
                county.setId(cursor.getInt(cursor.getColumnIndex("id")));
                county.setCountyName(cursor.getString(cursor.getColumnIndex("county_name")));
                county.setCountyCode(cursor.getString(cursor.getColumnIndex("county_code")));
                county.setCityId(cityId);

                counties.add(county);
            } while (cursor.moveToNext());
        }
        return counties;
    }

}
